package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.CmfzGuru;
import com.baizhi.service.CmfzGuruService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("guru")
public class CmfzGuruController {
    @Autowired
    private CmfzGuruService cmfzGuruService;
    @RequestMapping("downloadAll")
    @ResponseBody
    public Map downloadAll(){
        Map map = new HashMap();
        try {
            cmfzGuruService.downloadAll();
            map.put("falg",true);
        } catch (IOException e) {
            map.put("falg",false);
            e.printStackTrace();
        }
        return map;
    }
    @RequestMapping("uploadAll")
    @ResponseBody
    public Map uploadAll(MultipartFile file) throws Exception {
        /**
         * 根据文件对象获取输入流
         */
        InputStream inputStream = file.getInputStream();
        /**
         * ImportParams 导入参数对象
         * 定义标题栏和表头数据
         */
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1);
        importParams.setHeadRows(1);
        /**
         * importExcel 导入方法
         * 参数1 流 读取要导入的文件
         * 参数2 要导入的实体类的类对象  上师对象的类对象
         * 参数3 导入参数对象
         *
         * 返回值 导入数据 直接封装为集合对象
         */
        List<CmfzGuru> cmfzGurus = ExcelImportUtil.importExcel(inputStream, CmfzGuru.class, importParams);
        Map map = new HashMap();
        try {
            for (CmfzGuru gurus : cmfzGurus) {
                System.out.println(gurus);
                cmfzGuruService.insert(gurus);
            }
            map.put("falg",true);
        }catch (Exception e){
            map.put("falg",false);
        }
        return map;
    }
    @RequestMapping("selectAllGurus")
    @ResponseBody
    @LogAnnotation(content = "查询所有上师信息",type = "查询")
    public Map selectAllGurns(Integer page,Integer limit,String nickname){
        HashMap map = new HashMap();
        Page<CmfzGuru> cmfzGuruPage = cmfzGuruService.selectAll(page, limit, nickname);
        map.put("code",0);
        map.put("msg","");
        map.put("count",cmfzGuruPage.getTotal());
        map.put("data",cmfzGuruPage.getRecords());
        return map;
    }
    @RequestMapping("insertGuru")
    @ResponseBody
    @LogAnnotation(content = "添加上师信息",type = "添加")
    public Map insertGuru(CmfzGuru cmfzGuru){
        HashMap map = new HashMap();
        try{
            int insert = cmfzGuruService.insert(cmfzGuru);
            if (insert>0){
                map.put("flag",true);
            }else{
                map.put("flag",false);
            }
        }catch (Exception e){
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("updateGuruStatus")
    @ResponseBody
    @LogAnnotation(content = "更新上师状态",type = "更新")
    public Map updateGuruStatus(CmfzGuru cmfzGuru){
        System.out.println(cmfzGuru);
        HashMap map = new HashMap();
        try{
            if (cmfzGuru.getGuruStatus()==0){
                cmfzGuru.setGuruStatus(1);
                int updateGuruStatus = cmfzGuruService.updateGuruStatus(cmfzGuru);
                if (updateGuruStatus>0){
                    map.put("flag",true);
                }else{
                    map.put("flag",false);
                }
            }else {
                cmfzGuru.setGuruStatus(0);
                int updateGuruStatus = cmfzGuruService.updateGuruStatus(cmfzGuru);
                if (updateGuruStatus>0){
                    map.put("flag",true);
                }else{
                    map.put("flag",false);
                }
            }
        } catch (Exception e){
            map.put("flag",false);
        }
        return map;
    }

}
